package com.bj58.mis.apm.server.statistics.service.impl;

import com.bj58.mis.apm.server.core.util.UnitUtil;
import com.bj58.mis.apm.server.core.util.UnitValue;
import com.bj58.mis.apm.server.statistics.dto.chart.ChartData;
import com.bj58.mis.apm.server.statistics.dto.storage.StorageInfo;
import com.bj58.mis.apm.server.statistics.mapper.StorageMapper;
import com.bj58.mis.apm.server.statistics.service.BaseService;
import com.bj58.mis.apm.server.statistics.service.StorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wangxw
 * @Date: 2018/7/2 10:39
 */
@Service
@Transactional
public class StorageServiceImpl extends BaseService<StorageMapper> implements StorageService {

    /**
     * 查询存储-总体情况
     * @return
     */
    @Override
    public List<StorageInfo> getTotalSize(int days, String userName){
        List<StorageInfo> list = getStorageSize(days, userName, false);
        String unit = null;
        for(StorageInfo storage : list){
            if(unit == null){
                UnitValue uv = UnitUtil.deal(storage.getTotal());
                unit = uv.getUint();
            }
            storage.setTotal(UnitUtil.dealByUnit(storage.getTotal(), unit));
            storage.setHbase(UnitUtil.dealByUnit(storage.getHbase(), unit));
            storage.setMetadata(UnitUtil.dealByUnit(storage.getMetadata(), unit));
            storage.setUnit(unit);
        }
        return list;
    }

    /**
     * 按天统计hbase存储增量，查询当前用户有权限的Cube所占用的HBase存储增量的变化
     * @param days 最近多少天
     * @return
     */
    @Override
    public ChartData getHBaseSize(int days, String userName){
        List<StorageInfo> list = getStorageSize(days, userName, true);
        ChartData cd = null;
        if(list.size() > 0) {
            Double max = 0D;
            for (StorageInfo storage : list) {
                Double d = storage.getHbase();
                max = Math.max(max , d > 0 ? d : - d);
            }
            UnitValue uv = UnitUtil.deal(max);
            String unit = uv.getUint();
            for (StorageInfo storage : list) {
                storage.setHbase(UnitUtil.dealByUnit(storage.getHbase(), unit));
                storage.setUnit(unit);
            }
            cd = ChartData.buildChartData(list, "date", new String[]{"hbase"}).addNames(null, null, new String[]{unit});
        }
        return cd;
    }

    /**
     * 按天统计元数据存储增量，查询当前用户有权限的Cube所占用的元数据存储增量的变化
     * @param days 最近多少天
     * @return
     */
    @Override
    public ChartData getMetadataSize(int days, String userName){
        List<StorageInfo> list = getStorageSize(days, userName, true);
        ChartData cd = null;
        if(list.size() > 0) {
            Double max = 0D;
            for (StorageInfo storage : list) {
                Double d = storage.getMetadata();
                max = Math.max(max , d > 0 ? d : - d);
            }
            UnitValue uv = UnitUtil.deal(max);
            String unit = uv.getUint();
            for (StorageInfo storage : list) {
                storage.setMetadata(UnitUtil.dealByUnit(storage.getMetadata(), unit));
                storage.setUnit(unit);
            }
            cd = ChartData.buildChartData(list, "date", new String[]{"metadata"}).addNames(null, null, new String[]{unit});
        }
        return cd;
    }

    /**
     * 通用查询方法
     * @param days
     * @param userName
     * @param increment
     * @return
     */
    private List<StorageInfo> getStorageSize(int days, String userName, boolean increment){
        List<StorageInfo> list = new ArrayList<>();
        if(super.isAdmin(userName)){
            list.addAll(increment ? this.mapper.getStorageIncrementSize(true, null, days) : this.mapper.getStorageSize(true, null, days));
        }else{
            List<String> cubeids = this.getAuthCubeIdList(userName);
            if(cubeids.size() > 0){
                list.addAll(increment ? this.mapper.getStorageIncrementSize(false, cubeids, days) : this.mapper.getStorageSize(false, cubeids, days));
            }
        }
        return list;
    }

}
